<template>
  <div class="product_con">
      <el-card class="box-card">
      <div slot="header" class="clearfix">
      <span>筛选搜索</span>
      <el-button style="float: right; padding: 3px 0" type="primary" size='small' @click='searchFun'>查询</el-button>
      <el-button style="float: right; padding: 3px 0" type="button" size='small' @click="resetFun">重置</el-button>
      </div>

      
      
      <el-form ref="form" :model="listQuery" label-width="80px" class="aaa">
      <el-form-item label="输入搜索">
        <el-input v-model="listQuery.name" placeholder="请输入商品名称" ></el-input>
      </el-form-item>
      <el-form-item label="商品货号">
        <el-input v-model="listQuery.product_sn" placeholder="请输入商品货号"></el-input>
      </el-form-item>

      <el-form-item label="商品分类">
          <el-cascader
            v-model="productCateValue"
            :options="productCateOptions"
          ></el-cascader>
        </el-form-item>

      <el-form-item label="上架状态">
        <el-select v-model="listQuery.publish_status" placeholder="全部">
          <el-option v-for="item in publishStatusOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品品牌">
          <el-select v-model="listQuery.brand_id" placeholder="全部">
            <el-option
              v-for="item in brandOptions"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="listQuery.verify_status" placeholder="全部">
          <el-option v-for="item in verifyStatusOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value">
          </el-option>
        </el-select>
      </el-form-item> 

     
      
      </el-form>
       
      </el-card>

      <el-card class="box-card">
      <div slot="header" class="clearfix">
      <span>数据列表</span>
      <el-button style="float: right; padding: 3px 0" type="button"  @click='searchFun'>添加</el-button>
      
      </div>
     </el-card>

    <el-table :data="productList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" class="product_img" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>
      <el-table-column label="价格/货号">
        <template slot-scope="scope">
          <div>价格：￥{{scope.row.price}}</div>
          <div>货号：{{scope.row.product_sn}}</div>
        </template>
      </el-table-column>
      <el-table-column label="标签">
        <template slot-scope="scope">
            <div>上架：{{scope.row.publish_status | publishType}}</div>
            <div>新品：{{scope.row.new_status | publishType}}</div>
            <div>推荐：{{scope.row.recommand_status | publishType}}</div>
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          {{ scope.row.sort | sortType}}
        </template>
      </el-table-column>
      <el-table-column label="销量">
        <template slot-scope="scope">
          {{ scope.row.sale | saleType}}
        </template>
      </el-table-column>
      <el-table-column label="审核状态">
        <template slot-scope="scope">
          {{ scope.row.verify_status | verifyType }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="box">
          <el-button  size="small" @click="editFun(scope.row.id)" class="bj"
            >编辑</el-button
          >
          <el-button type="danger"  size="small" @click="delFun(scope.row.id)" class="bj"
            >删除</el-button
          >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
// import axios from "axios";
import {fetchProductList,fetchBrandList,fetchCateList,deleteProduct} from "@/api/product"  
export default {
  name: "product",
  data: function () {
    return {
      productList: [],
      listQuery:{
        pageSize:10,
        pageNum:1,
        name:'',
        product_sn: null,
        publish_status: null,
        brand_id:null,
        product_category_id: null
      },
      brandOptions:[],
      publishStatusOptions: [
       { value: 1, label: "上架" },
       { value: 0, label: "下架" },
     ],
     verifyStatusOptions:[
       {value:1,label:'已审核'},
       {value:0,label:'未审核'}
     ],
      productCateOptions: [], //商品分类option的数据
      productCateValue: null,
      total:0
    };
  },
  mounted() {
    // axios
    //   .get("http://192.168.6.250:8001/index/Api/product_list")
    //   .then((res) => {
    //     this.productList = res.data.product;
    //   });
    this.getProductData();
    this.getBrandData();
    this.getCateData()
  },
  
  filters: {
    verifyType(value) {
      // console.log(value);
      return value ? "已审核" : "未审核";
    },
    sortType(value){
      // console.log(value);
      return value ? "10" : "0"   ;},
    saleType(value){
      // console.log(value);
      return value ? "1" : "0";
    },
    publishType(value){
      console.log(value);
      return value ? "是" : "否"
    }
  },
  methods: {
    delFun(id){
      console.log('删除触发了',id);
       this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getProductData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    resetFun() {
      this.listQuery = {
        pageSize: 10, //一页10条数据
        pageNum: 1, //当前页
        name: "", //商品名称
        product_sn: null, //货号
        publish_status: null, //上架状态
        brand_id: null, //品牌id
        product_category_id: null, //分类id
      };
      this.productCateValue = null;
      this.getProductData();
    },
      editFun(id) {
      console.log("修改触发了", id);
      this.$router.push("/pms/productupdate/" + id);
    },
      handleSizeChange(val){
        this.listQuery.pageSize = val;
        this.listQuery.pageNum = 1;
        this.getProductData()
      },
      handleCurrentChange(val) {
      console.log("修改当前页的页码 触发了", val);
      this.listQuery.pageNum = val;
      this.getProductData();
    },
     searchFun(){
        this.getProductData();
      },
      async getProductData(){
        var res=await fetchProductList(this.listQuery)
        this.productList=res.product
        this.total=res.total
      },
      async getBrandData(){
        var res=await fetchBrandList()
        this.brandOptions=res.data
      },

      async getCateData() {
      var res = await fetchCateList();
      console.log(res);
      var data = res.data;
      data.forEach((i) => {
        let children = []; 
        i.children.forEach((j) => {
          children.push({
            label: j.name,
            value: j.id,
          });
        });
        this.productCateOptions.push({
          label: i.name,
          value: i.id,
          children: children,
        });
      });
      console.log(this.productCateOptions);
    },
  
     
  },
  
};
</script>

<style scoped lang="scss">
// .bj {
//   width: 50px;
// }
.box{
  display: flex;
}
.aaa{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.box-card .el-input{
  width: 150px;
}
.box-card .el-select{
  width: 150px;
}
.product_con {
  margin: 20px;
  .product_img {
    width: 100px;
  }
}
.btn1{
  background-color: white;
  color: black;
  margin-right: 5px;
}
.btn2{
  background-color: red;
  color: white;
}
.btn1,.btn2{
  width: 40px;
  height: 30px;
  border: 1px solid rgb(241, 240, 240);
  border-radius: 3px;
  font-size: 12px;
  
}
.tou{
  border: 1px solid rgb(241, 236, 236);
  border-bottom: none;
  display: flex;
  justify-content: space-between;
  height: 50px;
  line-height: 50px;
  color: rgb(180, 177, 177);
}
.bt1,.bt2{
  width: 50px;
  border: 1px solid rgb(248, 243, 243);
  border-radius: 3px;
  font-size: 12px;
}
.bt1{
  background-color: white;
}
.bt2{
  background-color: blue;
  color: white;
  margin-left: 5px;
}
.shuru{
  display: flex;
  justify-content: space-around;
  height: 50px;
  align-items: center;
  // border-bottom: 1px solid red;
  
}
.shenhe{
  display: flex;
  justify-content: center;
  height: 50px;
  
}
.tou2{
  border: 1px solid rgb(241, 236, 236);
  margin-bottom: 10px;
  font-size: 12px;
  color: rgb(180, 177, 177);
}
.srk{
  width: 80px;
  height: 25px;
  border: rgb(248, 243, 243) 1px solid ;
  border-radius: 5px;
  font-size: 12px;
}
.xl{
   width: 80px;
  height: 25px;
  border: rgb(248, 243, 243) 1px solid ;
  border-radius: 5px;
  font-size: 12px;
}
</style>